﻿using JNPF.Saas.Models.BaseTenant.Entities;
using JNPF.Utils;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JNPF.Saas.Logic.Tenant
{
    /// <summary>
    /// 租户信息
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2020.12.21 
    /// </summary>
    public class TenantBll
    {
        private TenantIService service = new TenantService();
        /// <summary>
        /// 列表(分页)
        /// </summary>
        /// <param name="input">参数</param>
        /// <returns></returns>
        public async Task<PageResult<TenantEntity>> GetList(PageInput input)
        {
            try
            {
                return await service.GetList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<TenantEntity>> GetList()
        {
            try
            {
                return await service.GetList();
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<TenantEntity> GetInfo(string id)
        {
            try
            {
                return await service.GetInfo(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="encode"></param>
        /// <returns></returns>
        public async Task<TenantEntity> GetDbContent(string phone)
        {
            try
            {
                return await service.GetDbContent(phone);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> Create(TenantEntity entity)
        {
            try
            {
                entity.Id = CommonHelper.Guid;
                entity.CreatorTime = DateTime.Now;
                entity.DbName = "jnpf_tenant_" + CommonHelper.Guid.Substring(0, 10);
                entity.ExpiresTime = DateTime.Now.AddMonths(1);
                entity.EnabledMark = 1;
                Task.Run(() =>
                {
                    this.CreateDB(entity.DbName);                  
                });
                return await service.Create(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        public async Task<int> Delete(TenantEntity entity, int flag)
        {
            try
            {
                if (flag == 1)
                {
                    await service.DeleteDB(entity);
                }
                return await service.Delete(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> Update(TenantEntity entity)
        {
            try
            {
                entity.LastModifyTime = DateTime.Now;
                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 创建租户数据库
        /// </summary>
        /// <param name="dbName"></param>
        /// <returns></returns>
        public Task CreateDB(string dbName)
        {
            try
            {
                return service.CreateDB(dbName);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

    }
}
